package com.joinlabs.action;

import java.math.BigDecimal;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.joinlabs.entity.CarProduct;
import com.joinlabs.entity.Shopcar;
import com.joinlabs.entity.ShopcarItem;
import com.joinlabs.entity.User;
import com.joinlabs.service.IShopcarService;

@Controller
@RequestMapping("/shopcar")
@SessionAttributes("user")
public class ShopcarAction {
	
	@Resource
	private IShopcarService shopcarServiceImpl;
	
	@RequestMapping("/show")	//显示购物车界面
	public ModelAndView show(@ModelAttribute("user") User user,ModelMap map) {
		String userId = user.getUserId();
		//System.out.println("============" + user);
		//1.查询购物车
		Shopcar shopcar = shopcarServiceImpl.findShopcarItemsByUserId(userId);
		//存入域对象
		map.addAttribute("shopcar",shopcar);
		//2.查询推荐商品
		List<CarProduct> list = shopcarServiceImpl.findRecommendWares();
		//存入域对象
		map.addAttribute("list",list);
		//3.跳转购物车界面
		if(shopcar == null || shopcar.getItems() == null || shopcar.getItems().size()==0){
			return new ModelAndView("/shopcar/noshopcar");
		}
		
		return new ModelAndView("/shopcar/shopcar");
		
	}
	
	@RequestMapping("/addcar")	//从购物车界面加入商品
	public ModelAndView insertFromShopcar(@ModelAttribute("user") User user,ShopcarItem shopcarItem) {
		//设置购买数量
		shopcarItem.setWaresCount(new BigDecimal(1));
		//设置用户Id
		shopcarItem.setUserId(user.getUserId());
		
		//生成购物车子项
		shopcarServiceImpl.createShopcar(shopcarItem);
		return new ModelAndView("redirect:/shopcar/show");
	}
	
	@RequestMapping("/addpro")	//从商品界面加入商品
	public ModelAndView insertFromProduct(@ModelAttribute("user") User user,ShopcarItem shopcarItem) {
		//设置购买数量
		shopcarItem.setWaresCount(new BigDecimal(1));
		//设置用户Id
		shopcarItem.setUserId(user.getUserId());
		
		//生成购物车子项
		shopcarServiceImpl.createShopcar(shopcarItem);
		return new ModelAndView("redirect:/shopcar/show");
	}
	
	@RequestMapping("/pluscount")	//购物车商品数量增加
	@ResponseBody
	public String plusConut(BigDecimal carId) {
		try {
			//数量增加
			shopcarServiceImpl.plusCountByCarId(carId);
			return "success";
		} catch (Exception e) {
			e.getStackTrace();
		}
		return "false";
	}
	
	@RequestMapping("/minuscount")	//购物车商品数量减少
	@ResponseBody
	public String minusConut(BigDecimal carId) {
		try {
			//数量减少
			shopcarServiceImpl.minusCountByCarId(carId);
			return "success";
		} catch (Exception e) {
			e.getStackTrace();
		}
		return "false";
	}
	
	@RequestMapping("/delitem")
	//减少库存
	public ModelAndView delItem(BigDecimal carId) {
		
		shopcarServiceImpl.delShopcarItemByCarId(carId);
		
		return new ModelAndView("redirect:/shopcar/show");
	}
	
}
